package com.vchecker.iobd_checker.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.vchecker.iobd_checker.R;

import org.w3c.dom.Text;

/**
 * Created by Yipb on 16-4-26.
 */

public class DlgUtils {
    public static VDialog showConfig(final Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dlg_ask_config, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // //0~255透明度值
        // v.getBackground().setAlpha(200);
        final VDialog alartDialog = new VDialog(context,
                R.style.edit_dialog_style);// 创建自定义样式dialog
        alartDialog.setCancelable(false);// 不可以用“返回键”取消
        alartDialog.setCanceledOnTouchOutside(false);// 不可以用“返回键”取消
        alartDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        alartDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    alartDialog.setResult(VDialog.RESULT_CANCEL);
                    alartDialog.dismiss();
                }
                return false;
            }
        });

        final Spinner spinnName = (Spinner)v.findViewById(R.id.spinName);
        final Spinner spinnLan = (Spinner)v.findViewById(R.id.spinLan);
        final CheckBox cbxUpgrad = (CheckBox)v.findViewById(R.id.cbxUpgrad);

        Button btnNext = (Button) v.findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(spinnName.getSelectedItemPosition() == 0){
                    Toast.makeText(context,"未选择产品型号",Toast.LENGTH_LONG).show();
                    return;
                }
                if(spinnLan.getSelectedItemPosition() == 0){
                    Toast.makeText(context,"未选择升级语言",Toast.LENGTH_LONG).show();
                    return;
                }
                alartDialog.setIntValue("Name",spinnName.getSelectedItemPosition());
                alartDialog.setIntValue("Lan",spinnLan.getSelectedItemPosition());
                if(cbxUpgrad.isChecked())
                    alartDialog.setIntValue("Upgrad",1);
                else
                    alartDialog.setIntValue("Upgrad",0);

                alartDialog.setResult(VDialog.RESULT_OK);
                alartDialog.dismiss();
            }
        });
        Button btnCancel = (Button) v.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_CANCEL);
                alartDialog.dismiss();
            }
        });
        return alartDialog;

    }


    public static VDialog showConnect(final Context context,boolean showNext) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dlg_ask_connect, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // //0~255透明度值
        // v.getBackground().setAlpha(200);
        final VDialog alartDialog = new VDialog(context,
                R.style.edit_dialog_style);// 创建自定义样式dialog
        alartDialog.setCancelable(false);// 不可以用“返回键”取消
        alartDialog.setCanceledOnTouchOutside(false);// 不可以用“返回键”取消
        alartDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        alartDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    alartDialog.setResult(VDialog.RESULT_CANCEL);
                    alartDialog.dismiss();
                }
                return false;
            }
        });

        TextView tvCount = (TextView)v.findViewById(R.id.tvCount);
        tvCount.setText(BluetoothAdapter.getDefaultAdapter().getBondedDevices().size()+"");

        Button btnConfig = (Button)v.findViewById(R.id.btnConfig);
        btnConfig.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_CONFIG);
                alartDialog.dismiss();
            }
        });

        Button btnNext = (Button) v.findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_OK);
                alartDialog.dismiss();
            }
        });
        if(!showNext){
            btnNext.setVisibility(View.GONE);
            View vline2 = (View)v.findViewById(R.id.vHline2);
            vline2.setVisibility(View.GONE);
        }
        Button btnCancel = (Button) v.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_CANCEL);
                alartDialog.dismiss();
            }
        });
        return alartDialog;

    }


    public static VDialog showOk(final Context context,String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dlg_ask_ok, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // //0~255透明度值
        // v.getBackground().setAlpha(200);
        final VDialog alartDialog = new VDialog(context,
                R.style.edit_dialog_style);// 创建自定义样式dialog
        alartDialog.setCancelable(false);// 不可以用“返回键”取消
        alartDialog.setCanceledOnTouchOutside(false);// 不可以用“返回键”取消
        alartDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        alartDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    alartDialog.setResult(VDialog.RESULT_CANCEL);
                    alartDialog.dismiss();
                }
                return false;
            }
        });

        TextView tvMsg = (TextView)v.findViewById(R.id.tvText);
        tvMsg.setText(msg);

        Button btnExit = (Button) v.findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_CANCEL);
                alartDialog.dismiss();
            }
        });

        Button btnContinue = (Button) v.findViewById(R.id.btnContinue);
        btnContinue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alartDialog.setResult(VDialog.RESULT_CONTINUE);
                alartDialog.dismiss();
            }
        });
        return alartDialog;

    }


}
